# Copyright 1999-2006 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: /var/cvsroot/gentoo-x86/x11-drivers/linuxwacom/linuxwacom-0.7.4_p3.ebuild,v 1.6 2006/09/06 03:03:24 metalgod Exp $

IUSE="gtk tcl tk usb wacom udev multitouch"

inherit eutils autotools linux-mod

DESCRIPTION="Input driver for Wacom tablets and drawing devices"
HOMEPAGE="http://linuxwacom.sourceforge.net/"
SRC_URI="mirror://sourceforge/${PN}/${P/_p/-}.tar.bz2"

LICENSE="GPL-2"
SLOT="0"
KEYWORDS="~amd64 ppc ~ppc64 x86"

RDEPEND="|| ( ( x11-proto/inputproto
		x11-base/xorg-server )
		  virtual/x11 )
	media-libs/libpixman
	gtk? ( >=x11-libs/gtk+-2 )
	tcltk? ( dev-lang/tcl dev-lang/tk )
	udev? ( sys-fs/udev )
	sys-libs/ncurses"

DEPEND="${RDEPEND}
	dev-util/pkgconfig
	usb? ( >=sys-kernel/linux-headers-2.6 )"
S=${WORKDIR}/${P/_p/-}

DRIVERPATH=$(find /lib/modules/${KV} -iname "wacom.ko" -printf "%h/")

wacom_check() {
	if use wacom ; then
		ebegin "Checking for wacom module"
		linux_chkconfig_module TABLET_USB_WACOM || linux_chkconfig_module USB_WACOM
		eend $?

		if [[ $? -ne 0 ]] || [ -z "$DRIVERPATH" ]; then
			eerror "You need to have your kernel compiled with wacom as module"
			eerror "to let linuxwacom overwrite it."
			eerror "Enable it in the kernel, found at:"
			eerror
			eerror " Device Drivers"
			eerror "     USB support"
			eerror "           <M> Wacom Intuos/Graphire tablet support"
			eerror
			eerror "(in the same page is suggested to include as modules also:"
			eerror "EHCI , OHCI , USB Human Interface Device+HID input layer)"
			eerror
			eerror "Then recompile kernel.Elsewhere, remove wacom flag,please."
			die "Wacom not compiled in kernel as a module!"
		fi
	fi
}

pkg_setup() {
	linux-mod_pkg_setup
	# echo "kernel version is ${KV} , name is ${KV%%-*}"
	wacom_check
}

src_unpack() {
	unpack ${A}
	cd ${S}

	# Fix multilib-strict error for Tcl/Tk library install
	sed -i -e "s:WCM_EXECDIR/lib:WCM_EXECDIR/$(get_libdir):" configure.in

	epatch ${FILESDIR}/${P%_p*}-pDev.patch
	if ! use multitouch ; then
		epatch ${FILESDIR}/${P%_p*}-untouch.patch
	fi

	eautoreconf
}

src_compile() {
	if use gtk; then
		myconf="--with-gtk=2.0"
	else
		myconf="--with-gtk=no"
	fi

	if use wacom; then
		myconf="${myconf} --enable-wacom"
		myconf="${myconf} --with-kernel=${KV_OUT_DIR}"
	fi

	econf ${myconf} \
		`use_with tcl tcl` \
		`use_with tk tk` \
		--enable-wacomdrv --enable-wacdump \
		--enable-xsetwacom --enable-dlloader || die

	unset ARCH
	emake || die "build failed."
}

src_install() {
	emake DESTDIR="${D}" install || die "Install failed."
	if use wacom; then
		insinto "$DRIVERPATH"
		#doins "${S}/src/${KV%%-*}/wacom.ko"
		kv=${KV%%-*}
		case $kv in
			2.4.*)
			kr=`expr $kv : '2\.4\.\(.*\)$'`
			kv=2.4
			while test $kr -gt 0; do
				if test -d "${S}/src/2.6.$kr"; then
				kv=2.6.$kr
				break
				fi
				: $((kr--))
			done
			;;
			2.4)
			kv=2.4
			;;
			2.6.*)
			kr=`expr $kv : '2\.6\.\(.*\)$'`
			kv=2.6
			while test $kr -gt 0; do
				if test -d "${S}/src/2.6.$kr"; then
				kv=2.6.$kr
				break
				fi
				: $((kr--))
			done
			;;
			2.6)
			kv=2.6
			;;
		esac
		doins "${S}/src/${kv}/wacom.ko"
	fi
	if use udev; then
		insinto /etc/udev/rules.d/
			doins ${FILESDIR}/60-wacom.rules
	fi
	dohtml -r docs/*
	dodoc AUTHORS ChangeLog NEWS README
}
